package com.jp.knowledge.logic;

import android.content.Context;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.TopicInfo;
import com.jp.knowledge.util.ResultSubscriber;

/**
 * Created by jiayen on 16/10/26.
 * 针对话题的统一逻辑处理  包括点赞 、 等
 */

public class TopicLogic implements ResultSubscriber.OnResultListener {
    private final int TOPIC_PRISE_CODE = 0x0000000000001;//话题点赞
    private final int GET_TOPIC_CODE = 0x0000000000002;//获取话题数据
    private Context mContext;
    private TopicLogicCallBack callback;
    private int position;//点赞的位置
    private TopicDataCallBack topicCallBack;

    public TopicLogic(Context mContext, TopicLogicCallBack callback) {
        this.mContext = mContext;
        this.callback = callback;
    }


    public TopicLogic(Context mContext) {
        this.mContext = mContext;
    }

    /**
     * 话题点赞
     */
    public void topicPrise(String infoId, String topicId, String commentId, int position) {
        this.position = position;
        JsonObject param = new JsonObject();
        param.addProperty("infoId", infoId);
        param.addProperty("topicId", topicId);
        param.addProperty("commentId", commentId);
        HttpHelper.getInstance(mContext).topicPraise(param, TOPIC_PRISE_CODE, this);
    }


    public void setTopicCallBack(TopicDataCallBack topicCallBack) {
        this.topicCallBack = topicCallBack;
    }

    /**
     * 获取话题数据
     */
    public void getTopicData(int type,String id) {
        //获取详情数据成功后  获取话题数据
        JsonObject param = new JsonObject();
        param.addProperty("id", id);
        param.addProperty("type", type);
        HttpHelper.getInstance(mContext).headlinesTopicDetails(param, GET_TOPIC_CODE, this);
    }

    @Override
    public void onStart(int requestType) {

    }

    @Override
    public void onCompleted(int requestType) {

    }

    @Override
    public void onError(int requestType) {

    }

    @Override
    public void onNext(IModel data, int requestType) {
        if (requestType == TOPIC_PRISE_CODE) {//处理话题点赞
            handTopicPrise(data);
        } else if (requestType == GET_TOPIC_CODE) {//获取话题数据成功
            if (topicCallBack != null) {
                if (data.getErrcode() == 0) {
                    topicCallBack.done(data.getEntity(TopicInfo.class));
                } else {
                    ToasUtil.toast(mContext, "获取话题数据失败");
                }

            }
        }
    }

    /**
     * 处理话题点赞
     *
     * @param data
     */
    private void handTopicPrise(IModel data) {
        int code = data.getErrcode();
        if (code == 0) {//点赞成功
            if (callback != null)
                callback.priseSuccess(position);
        } else if (code == 120104) {
            ToasUtil.toast(mContext, "你已赞过!");
        } else {//失败
            ToasUtil.toast(mContext, "请求有误，请稍后再试!");
        }
    }


    public interface TopicDataCallBack {
        void done(TopicInfo topic);
    }
}
